<?php
/*
 * 监视更新
 */
class ChecknewController extends TController {
	/*
	 * 拍拍英爱浪莎店铺 监视页面列表
	 * 店铺地址：http://shop.paipai.com/1103143365
	 */
	private $yinai_url = "http://shop.paipai.com/1103143365";
	private $yinai_listurl = array(
		array('505640697','0-00000000033-0-1-1-0-3-0-0-0/index.shtml?PTAG=40042.1.5','50006846'),
		array('511163443','0-00000000034-0-1-1-0-3-0-0-0/index.shtml?PTAG=40042.1.5','50006846'),
		array('511163444','0-00000000035-0-1-1-0-3-0-0-0/index.shtml?PTAG=40042.1.5','50006846'),
		array('511163445','0-00000000036-0-1-1-0-3-0-0-0/index.shtml?PTAG=40042.1.5','50006846'),
		array('511163617','0-00000000038-0-1-1-0-3-0-0-0/index.shtml?PTAG=40042.1.5','50006846'),
		array('511163618','0-00000000039-0-1-1-0-3-0-0-0/index.shtml?PTAG=40042.1.5','50006846'),
		array('511163619','0-00000000040-0-1-1-0-3-0-0-0/index.shtml?PTAG=40042.1.5','50006846'),
		array('511164120','0-00000000042-0-1-1-0-3-0-0-0/index.shtml?PTAG=40042.1.5','50008882'),
		array('511164121','0-00000000043-0-1-1-0-3-0-0-0/index.shtml?PTAG=40042.1.5','50008882'),
		array('511164122','0-00000000200-0-1-1-0-3-0-0-0/index.shtml?PTAG=40042.1.5','50012778'),
		array('511164123','0-00000000001-0-1-1-0-3-0-0-0/index.shtml?PTAG=40042.1.5','50012778'),
		array('511164124','0-00000000046-0-1-1-0-3-0-0-0/index.shtml?PTAG=40042.1.5','50010394'),
		array('511164125','0-00000000047-0-1-1-0-3-0-0-0/index.shtml?PTAG=40042.1.5','50010394'),
		);
		
	/*
	 * 磐安浪莎批发店铺监视更新
	 */
	 private $pals_url = "http://langsha01.taobao.com/";	 
	 private $pals_listurl = array(
		array('505640697','search.htm?scid=476380473&scname=way%2F483g&checkedRange=true&queryType=cat','50006846'),
		array('511163443','search.htm?scid=476380474&scname=vsW31r%2Fj&checkedRange=true&queryType=cat','50006846'),
		array('511163444','search.htm?scid=476804712&scname=ssi9xb%2FjzeA%3D&checkedRange=true&queryType=cat','50006846'),
		array('511163445','search.htm?scid=499478332&scname=tqy%2F7rTytde%2F4w%3D%3D&checkedRange=true&queryType=cat','50006846'),
		array('511163617','search.htm?scid=476394195&scname=xa62zMu%2FzeA%3D&checkedRange=true&queryType=cat','50006846'),
		array('511163617','search.htm?scid=476394196&scname=xNDLv83g&checkedRange=true&queryType=cat','50006846'),
		array('511163618','search.htm?scid=476380471&scname=xa7D3s3g&checkedRange=true&queryType=cat','50006846'),
		array('511163618','search.htm?scid=476380472&scname=xNDD3s3g&checkedRange=true&queryType=cat','50006846'),	
		array('511164120','search.htm?scid=476394194&scname=xa7Kv7bMv%2BM%3D&checkedRange=true&queryType=cat','50008882'),
		array('511164121','search.htm?scid=476394197&scname=xNDKv7bMv%2BM%3D&checkedRange=true&queryType=cat','50008882'),	
		array('511164124','search.htm?scid=479704624&scname=xNDFrrGz0MQ%3D&checkedRange=true&queryType=cat','50010394'),
		
		);

	/*
	 * 监视磐安浪莎批发店铺
	 */
	 public function actionPals($p = 0) {
	 	if($p>count($this->pals_listurl)){
			echo "没有地址了";
			Yii::app()->end();
		}
		$url = $this->pals_url.'/'.$this->pals_listurl[$p][1];		
		$snoopy = new Snoopy;
		$snoopy->fetch($url);
		$html = iconv("gb2312","utf-8",$snoopy->results);
		$html = $this->get_content($html,'pagination-mini','class="pagination"');
		$split_html = explode('class="item"',$html);
		
		foreach($split_html as $list){
		$outorderid = '01_'.$this->get_content($list,'item.htm?id=','&"');
		$img = $this->get_content($list,'data-ks-lazyload="','_160');
		$title = trim(strip_tags($this->get_content($list,'class="desc">','</a>')));
		$price = trim(str_replace('元','',$this->get_content($list,'<strong>','</strong>')))*100;
			if ($title) {
				$itme = Itmes::model()->find(array(
					"condition"=>"uid={$this->user->uid} and outer_id='{$outorderid}' "
					));
				if ($itme) {
					$cz = "已经存在";
				}else{
					$cz = "新增";
					$itme = new Itmes('add');
					$itme->top_session = $this->user->top_session;			
					$itme->attributes = array(
						'num' => 9999, 
						'price' => $price, 
						'type' => 'fixed',
						'stuff_status' => 'new',
						'title' => $title,
						'desc' => '还没有描述',
						'location_city' => '金华',
						'location_state' => '浙江',
						'cid' => $this->pals_listurl[$p][2],
						'seller_cids' => $this->pals_listurl[$p][0],
						'uid' => $this->user->uid,
						'approve_status'=>'instock',
						'props' => $x_props,
						'outer_id' => $outorderid,
						);			
					$resp = $itme->addapi();
					if($resp){
												
						$putfile = '/runtime/upfile/'.date("Ymd");
						if(!is_dir($_SERVER[DOCUMENT_ROOT].$putfile)) mkdir($_SERVER[DOCUMENT_ROOT].$putfile);
						$img = $putfile.'/'.time().rand(1000,9999).'.jpg';
						
						$handle = @fopen($_SERVER[DOCUMENT_ROOT].$img, "w");
						@fwrite($handle, @file_get_contents($httpimg));
						@fclose($handle);
						$req = new ItemImgUploadRequest;
						$req->setNumIid($resp->item->num_iid);
						$req->setImage("@".$_SERVER[DOCUMENT_ROOT].$img);
						$req->setPosition(1);
						$req->setIsMajor("true");
						$respx = Yii::app()->top->execute($req, $this->user->top_session);
					
						$imgs = new ItemsImg();						
						$imgs->num_iid = $resp->item->num_iid;						
						$imgs->img = $img;
						$imgs->price = $price;
						$imgs->save();
						
						$itme->num_iid = $resp->item->num_iid;				
						$itme->uptime = time();
						$itme->modified = time();
						$itme->pic_url = $respx->item_img->url;
						$itme->save();
						print_r($itme->errors);
						print_r($resp);
						echo "下载图片：$httpimg";
					}}
				echo "{$cz}：$title \r\n";
			}
				
		}
		
	 }
	/*
	 * 监视英爱拍拍店铺宝贝更新
	 */
	public function actionYinai($p = 0) {		
		
		if($p>count($this->yinai_listurl)){
			echo "没有地址了";
			Yii::app()->end();
		}
		
		$url = $this->yinai_url.'/'.$this->yinai_listurl[$p][1];		
		$snoopy = new Snoopy;
		$snoopy->fetch($url);
		$html = iconv("gb2312","utf-8",$snoopy->results);
		$html = $this->get_content($html,'pfhlkd_glist','pfhlkd_toolbar');
		$split_html = explode("commodityid",$html);
				
		foreach($split_html as $list) {
			$outorderid = '02'.mb_substr($this->get_content($list,'auction1.paipai.com/','" target='),-10,10);			
			$httpimg = $this->get_content($list,'init_src="','.120x120').'.jpg';
			$title = trim(strip_tags($this->get_content($list,'class="title">','</a>')));
			$price = trim(str_replace('&yen;','',$this->get_content($list,'<p class="price">','</p>')))*100;			
			if ($title) {
				$itme = Itmes::model()->find(array(
					"condition"=>"outer_id='{$outorderid}' "
					));
				if ($itme) {
					$cz = "已经存在";
				}else{
					$cz = "新增";
					$itme = new Itmes('add');
					$itme->top_session = $this->user->top_session;	
					switch($p){
						case 5:
							$propss = '2097794:20533';
							break;
						case 7:
							$propss = '2097794:20533';
							break;
					}
					$itme->attributes = array(
						'num' => 9999, 
						'price' => $price, 
						'type' => 'fixed',
						'stuff_status' => 'new',
						'title' => $title,
						'desc' => '还没有描述',
						'location_city' => '金华',
						'location_state' => '浙江',
						'cid' => $this->yinai_listurl[$p][2],
						'seller_cids' => $this->yinai_listurl[$p][0],						
						'approve_status'=>'instock',
						'props' => $x_props,
						'outer_id' => $outorderid,
						
						'props'=> $propss,
						);			
					$resp = $itme->addapi();
					if($resp){
												
						$putfile = '/runtime/upfile/'.date("Ymd");
						if(!is_dir($_SERVER[DOCUMENT_ROOT].$putfile)) mkdir($_SERVER[DOCUMENT_ROOT].$putfile);
						$img = $putfile.'/'.time().rand(1000,9999).'.jpg';
						
						$handle = @fopen($_SERVER[DOCUMENT_ROOT].$img, "w");
						@fwrite($handle, @file_get_contents($httpimg));
						@fclose($handle);
						$req = new ItemImgUploadRequest;
						$req->setNumIid($resp->item->num_iid);
						$req->setImage("@".$_SERVER[DOCUMENT_ROOT].$img);
						$req->setPosition(1);
						$req->setIsMajor("true");
						$respx = Yii::app()->top->execute($req, $this->user->top_session);
					
						$imgs = new ItemsImg();						
						$imgs->num_iid = $resp->item->num_iid;						
						$imgs->img = $img;
						$imgs->price = $price;
						$imgs->save();
						
						$itme->num_iid = $resp->item->num_iid;				
						$itme->uptime = time();
						$itme->modified = time();
						$itme->pic_url = $respx->item_img->url;
						$itme->save();
						print_r($itme->errors);
						print_r($resp);
						echo "下载图片：$httpimg";
					}
				}
				echo "{$cz}：$title \r\n";
			}			
			
		}
		//end this function
	}
	
	
	/*
	 * html cute
	 */
	private function get_content($str, $start, $end){
        if ( $start == '' || $end == '' ){
               return;
        }
        $str = explode($start, $str);
        $str = explode($end, $str[1]);
		$html = $str[0];
        return $html;
	}
	
}














